package Fulbito.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import Fulbito.EXCEPCION.DAOExcepcion;
import Fulbito.MODELO.Cancha;
import Fulbito.MODELO.Reserva;
import Fulbito.UTIL.ConexionBD;

public class ReservaDAO extends BaseDAO {

	public Collection<Reserva> ListarReservas(Reserva ientReserva) throws DAOExcepcion {
		String query = "SELECT CH.CodCancha,CH.Nombre Cancha,LO.CodLocal, LO.Nombre Local,CH.Descripcion,LO.Direccion,LO.DireccionGoogleMaps,LO.TelefonoFijo,";
		query = query + "Dias,Hora,CH.Estado FROM Cancha CH ";
		query = query + "INNER JOIN Local LO ON CH.Local_CodLocal = LO.CodLocal ";
		query = query + "LEFT JOIN Alquiler AL ON AL.Cancha_CodCancha = CH.CodCancha ";
		query = query + "WHERE LO.CodLocal = ? AND CodCancha= ? ";
		Collection<Reserva> lista = new ArrayList<Reserva>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, ientReserva.getCodCancha());
			stmt.setInt(2, ientReserva.getLocal_CodLocal());
			rs = stmt.executeQuery();
			while (rs.next()) {
				Reserva entReserva = new Reserva();
				entReserva.setCodCancha(rs.getInt("CodCancha"));
				entReserva.setCancha(rs.getString("Cancha"));
				entReserva.setLocal_CodLocal(rs.getInt("CodLocal"));
				entReserva.setLocal(rs.getString("Local"));
				entReserva.setDescripcion(rs.getString("Descripcion"));
				entReserva.setDireccion(rs.getString("Direccion"));
				entReserva.setDireccionGoogleMaps(rs.getString("DireccionGoogleMaps"));
				entReserva.setTelefonoFijo(rs.getString("TelefonoFijo"));
				entReserva.setDias(rs.getString("Dias"));
				entReserva.setHora(rs.getString("Hora"));
				entReserva.setEstado(rs.getString("Estado"));
				lista.add(entReserva);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}
}
